package com.api.shopapi.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.mapper.ElderlyCheckinAssessMapper;
import com.api.shopapi.service.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service("ElderlyCheckinAssessServiceImplOne")
public class ElderlyCheckinAssessServiceImpl implements ElderlyCheckinAssessService {
    @Autowired
    private ElderlyCheckinAssessMapper elderlyCheckinAssessMapper;

    @Autowired
    @Qualifier("ElderlyCheckinAssessItemServiceImplOne")
    private ElderlyCheckinAssessItemService elderlyCheckinAssessItemService;

    @Autowired
    @Qualifier("CheckinAssessItemServiceImplOne")
    private CheckinAssessItemService checkinAssessItemService;

    @Autowired
    @Qualifier("CheckinAssessGroupServiceImplOne")
    private CheckinAssessGroupService checkinAssessGroupService;

    @Autowired
    @Qualifier("CheckinAssessClassServiceImplOne")
    private CheckinAssessClassService checkinAssessClassService;

    @Override
    @Transactional
    public int insert(ElderlyCheckinAssess elderlyCheckinAssess, JSONArray assessArr) {
        String elderlyCheckinAssessCode = elderlyCheckinAssess.getElderlyCheckinAssessCode();
        int fraction = AddCheckin(assessArr, elderlyCheckinAssessCode);
        elderlyCheckinAssess.setFraction(fraction);
        return elderlyCheckinAssessMapper.insert(elderlyCheckinAssess);
    }


    @Override
    public int insert2(ElderlyCheckinAssess elderlyCheckinAssess) {
        return elderlyCheckinAssessMapper.insert(elderlyCheckinAssess);
    }


    @Override
    public int updateByElderlyCheckinAssessCode(ElderlyCheckinAssess elderlyCheckinAssess, JSONArray assessArr) {
        String elderlyCheckinAssessCode = elderlyCheckinAssess.getElderlyCheckinAssessCode();
        elderlyCheckinAssessItemService.deleteByElderlyCheckinAssessCode(elderlyCheckinAssessCode);

        int fraction = AddCheckin(assessArr, elderlyCheckinAssessCode);
        elderlyCheckinAssess.setFraction(fraction);
        return elderlyCheckinAssessMapper.updateByElderlyCheckinAssessCode(elderlyCheckinAssess);
    }

    private int AddCheckin(JSONArray assessArr, String elderlyCheckinAssessCode) {
        int fractionSum = 0;
        for (Object i : assessArr) {
            if(i!=null){
                JSONObject jsonObject1 = (JSONObject) i;
                String classCode = jsonObject1.getString("classCode");
                JSONArray itemArrCode = jsonObject1.getJSONArray("itemArrCode");

                CheckinAssessClass checkinAssessClass = checkinAssessClassService.getByCheckinAssessClassCode(classCode);
                String checkinAssessClassName = checkinAssessClass.getName();

                ElderlyCheckinAssessItem elderlyCheckinAssessItem = new ElderlyCheckinAssessItem();

                for (Object j : itemArrCode) {
                    String checkinAssessItemCode = (String) j;
                    CheckinAssessItem checkinAssessItem = checkinAssessItemService.getByCheckinAssessItemCode(checkinAssessItemCode);

                    String checkinAssessGroupCode = checkinAssessItem.getCheckinAssessGroupCode();
                    CheckinAssessGroup checkinAssessGroup = checkinAssessGroupService.getByCheckinAssessGroupCode(checkinAssessGroupCode);

                    int fraction = checkinAssessItem.getFraction();
                    fractionSum = fractionSum + fraction;

                    elderlyCheckinAssessItem.setElderlyCheckinAssessItemCode(UUID.randomUUID().toString());
                    elderlyCheckinAssessItem.setElderlyCheckinAssessCode(elderlyCheckinAssessCode);
                    elderlyCheckinAssessItem.setCheckinAssessGroupCode(checkinAssessGroupCode);
                    elderlyCheckinAssessItem.setCheckinAssessGroupName(checkinAssessGroup.getName());
                    elderlyCheckinAssessItem.setCheckinAssessItemCode(checkinAssessItemCode);
                    elderlyCheckinAssessItem.setCheckinAssessItemName(checkinAssessItem.getName());
                    elderlyCheckinAssessItem.setFraction(checkinAssessItem.getFraction());
                    elderlyCheckinAssessItem.setCheckinAssessClassCode(classCode);
                    elderlyCheckinAssessItem.setCheckinAssessClassName(checkinAssessClassName);
                    elderlyCheckinAssessItemService.insert(elderlyCheckinAssessItem);
                }
            }

        }
        return fractionSum;

    }

    @Override
    public int deleteByElderlyCheckinAssessCode(String elderlyCheckinAssessCode) {
        return elderlyCheckinAssessMapper.deleteByElderlyCheckinAssessCode(elderlyCheckinAssessCode);
    }


    @Override
    public ElderlyCheckinAssess getByElderlyCheckinAssessCode(String elderlyCheckinAssessCode) {
        return elderlyCheckinAssessMapper.getByElderlyCheckinAssessCode(elderlyCheckinAssessCode);
    }

    @Override
    public ElderlyCheckinAssess getByElderlyCode(String elderlyCode) {
        return elderlyCheckinAssessMapper.getByElderlyCode(elderlyCode);
    }

    @Override
    public PageInfo<ElderlyCheckinAssess> getPageList(int pageIndex, int pageSize, String institutionCode,String elderlyName) {
        PageHelper.startPage(pageIndex, pageSize);
        List<ElderlyCheckinAssess> elderlyCheckinAssessList = elderlyCheckinAssessMapper.getPageList(institutionCode,elderlyName);
        PageInfo<ElderlyCheckinAssess> pageInfo = new PageInfo<>(elderlyCheckinAssessList);
        return pageInfo;
    }

    @Override
    @Transactional
    public int insertAndItem(ElderlyCheckinAssess elderlyCheckinAssess, JSONArray assessArr, int isSleep) {
        String elderlyCheckinAssessCode = elderlyCheckinAssess.getElderlyCheckinAssessCode();
        int num1 = 0; //日常生活活动能力
        int num2 = 0; //精神状态与社会参与能力
        int num3 = 0; //感知觉与沟通能力
        int num4 = 0; //老年综合征罹患情况

        for (Object i : assessArr) {
            JSONObject jsonObject1 = (JSONObject) i;
            String classCode = jsonObject1.getString("classCode");
            JSONArray itemArrCode = jsonObject1.getJSONArray("itemArrCode");

            CheckinAssessClass checkinAssessClass = checkinAssessClassService.getByCheckinAssessClassCode(classCode);
            String checkinAssessClassName = checkinAssessClass.getName();

            ElderlyCheckinAssessItem elderlyCheckinAssessItem = new ElderlyCheckinAssessItem();
            int fractionSum = 0;
            for (Object j : itemArrCode) {
                String checkinAssessItemCode = (String) j;
                CheckinAssessItem checkinAssessItem = checkinAssessItemService.getByCheckinAssessItemCode(checkinAssessItemCode);

                String checkinAssessGroupCode = checkinAssessItem.getCheckinAssessGroupCode();
                CheckinAssessGroup checkinAssessGroup = checkinAssessGroupService.getByCheckinAssessGroupCode(checkinAssessGroupCode);

                int fraction = checkinAssessItem.getFraction();
                fractionSum = fractionSum + fraction;

                elderlyCheckinAssessItem.setElderlyCheckinAssessItemCode(UUID.randomUUID().toString());
                elderlyCheckinAssessItem.setElderlyCheckinAssessCode(elderlyCheckinAssessCode);
                elderlyCheckinAssessItem.setCheckinAssessGroupCode(checkinAssessGroupCode);
                elderlyCheckinAssessItem.setCheckinAssessGroupName(checkinAssessGroup.getName());
                elderlyCheckinAssessItem.setCheckinAssessItemCode(checkinAssessItemCode);
                elderlyCheckinAssessItem.setCheckinAssessItemName(checkinAssessItem.getName());
                elderlyCheckinAssessItem.setFraction(checkinAssessItem.getFraction());
                elderlyCheckinAssessItem.setCheckinAssessClassCode(classCode);
                elderlyCheckinAssessItem.setCheckinAssessClassName(checkinAssessClassName);
                elderlyCheckinAssessItemService.insert(elderlyCheckinAssessItem);

            }
            //如果判断为昏迷，直接设为等级3（重度昏迷）
            if (checkinAssessClassName.equals("日常生活活动能力")) {
                num1 = fractionSum;
            } else if (checkinAssessClassName.equals("精神状态与社会参与能力")) {
                num2 = fractionSum;
            } else if (checkinAssessClassName.equals("感知觉与沟通能力")) {
                num3 = fractionSum;
            } else if (checkinAssessClassName.equals("老年综合征罹患情况") && checkinAssessClass.getCalculationType() == 2) {
                num4 = fractionSum;
            }

        }
        //判断护理等级
        if (num4 > 5) { //如果罹患选项大于5，
            elderlyCheckinAssess.setCheckinAssessLevel(5);
        } else {
            if (isSleep == 2) {//如果为昏迷状态，直接设为3
                elderlyCheckinAssess.setCheckinAssessLevel(4);
            } else {
                int grade = grade(num1,num2,num3,num4);
                elderlyCheckinAssess.setCheckinAssessLevel(grade);
            }
        }
        elderlyCheckinAssess.setFraction(num1+num2+num3+num4);
        return elderlyCheckinAssessMapper.insert(elderlyCheckinAssess);
    }


    public int grade(int num1, int num2, int num3, int num4) {
        //0:完好,1:轻度受损,2:中度受损,3:重度受损
        int e = 0; //对应num2
        int f = 0; //对应num3
        //根据日常生活活动能力为基准
        if (num1 == 0) {
            if (num2 >= 0 && num2 <= 8) {
                e = 0;
            }
            if (num2 >= 9 && num2 <= 40) {
                e = 1;
            }
            if (num3 >= 0 && num3 <= 4) {
                f = 0;
            }
            if (num3 >= 5 && num3 <= 12) {
                f = 1;
            }

        } else if (num1 >= 1 && num1 <= 20) { //1到20分判断
            if (num2 >= 0 && num2 <= 8) {
                e = 1;
            }
            if (num2 >= 9 && num2 <= 40) {
                e = 2;
            }
            if (num3 >= 0 && num3 <= 4) {
                f = 1;
            }
            if (num3 >= 5 && num3 <= 12) {
                f = 2;
            }
        } else if (num1 >= 21 && num1 <= 40) { //21到40分判断
            if (num2 >= 0 && num2 <= 24) {
                e = 2;
            }
            if (num2 >= 25 && num2 <= 40) {
                e = 3;
            }
            if (num3 >= 0 && num3 <= 8) {
                f = 2;
            }
            if (num3 >= 9 && num3 <= 12) {
                f = 3;
            }
        } else if (num1 >= 41 && num1 <= 60) { //41到60分判断
            e = 3;
            f = 3;
        }

        int h = e > f ? e : f;
        //判断护理需求等级
        if (h == 0) {
            if (num4 >= 0 && num4 <= 2) {
                return 1;
            } else {
                return 2;
            }
        } else if (h == 1) {
            if (num4 >= 0 && num4 <= 2) {
                return 2;
            } else {
                return 3;
            }
        } else if (h == 2) {
            if (num4 >= 0 && num4 <= 2) {
                return 3;
            } else {
                return 4;
            }
        } else {
            return 5;
        }
    }


    @Override
    public
    PageInfo<ElderlyCheckinAssess> pageByGovCode(int pageIndex, int pageSize, String governmentCode,String elderlyName){
        PageHelper.startPage(pageIndex, pageSize);
        List<ElderlyCheckinAssess> elderlyCheckinAssessList = elderlyCheckinAssessMapper.pageByGovCode(governmentCode,elderlyName);
        PageInfo<ElderlyCheckinAssess> pageInfo = new PageInfo<>(elderlyCheckinAssessList);
        return pageInfo;
    }


    @Override
    public
    PageInfo<ElderlyCheckinAssess> pageByElderlyCode(int pageIndex, int pageSize, String eldelryCode){
        PageHelper.startPage(pageIndex, pageSize);
        List<ElderlyCheckinAssess> elderlyCheckinAssessList = elderlyCheckinAssessMapper.listByElderlyCode(eldelryCode);
        PageInfo<ElderlyCheckinAssess> pageInfo = new PageInfo<>(elderlyCheckinAssessList);
        return pageInfo;
    }

    @Override
    public
    PageInfo<ElderlyCheckinAssess> pageByMedCode(int pageIndex, int pageSize, String medicinerCode){
        PageHelper.startPage(pageIndex, pageSize);
        List<ElderlyCheckinAssess> elderlyCheckinAssessList = elderlyCheckinAssessMapper.listByMedicinerCode(medicinerCode);
        PageInfo<ElderlyCheckinAssess> pageInfo = new PageInfo<>(elderlyCheckinAssessList);
        return pageInfo;
    }

    @Override
    public
    PageInfo<ElderlyCheckinAssess> pageByNurseCode(int pageIndex, int pageSize, String nurseCode){
        PageHelper.startPage(pageIndex, pageSize);
        List<ElderlyCheckinAssess> elderlyCheckinAssessList = elderlyCheckinAssessMapper.listByNurseCode(nurseCode);
        PageInfo<ElderlyCheckinAssess> pageInfo = new PageInfo<>(elderlyCheckinAssessList);
        return pageInfo;
    }


}
